package com.thinkin.ordercenter.buz;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkin.ordercenter.dao.OchannelDao;
import com.thinkin.ordercenter.dao.impl.OchannelDaoImpl;
import com.thinkin.ordercenter.po.Ochannel;
import com.thinkin.ordercenter.util.OchannelUtil;
import com.thinkin.ordercenter.vo.OchannelVo;

public class OchannelDaoBuz {
	Logger logger = LoggerFactory.getLogger(OchannelDaoBuz.class);
	private OchannelDao ochannelDao = null;

	public OchannelDaoBuz() {
		ochannelDao = new OchannelDaoImpl();
	}

	public String add(Ochannel ochannel) {
		String sql = "insert into O_CHANNEL(" + "KEYID,USERNAME,"
				+ "PWD,DSNAME," + "DSTYPE,DSSHORTCODE," + "DSCODE,AUTHBRAND,"
				+ "PRODUCTCATEGORY," + "BAIL,DBUSINESSUSER,"
				+ "DBUSINESSMODEL," + "CREATENAME,CREATEDATE"
				+ ") values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Object param[] = { ochannel.getKeyid(), ochannel.getUsername(),
				ochannel.getPwd(), ochannel.getDsname(), ochannel.getDstype(),
				ochannel.getDsshortcode(), ochannel.getDscode(),
				ochannel.getAuthbrand(), ochannel.getProductcategory(),
				ochannel.getBail(), ochannel.getDbusinessuser(),
				ochannel.getDbusinessmodel(), ochannel.getCreatename(),
				ochannel.getCreatedate() };
		// ,CREATEDATE ,? "to_date(" + ochannel.getStrsCreatedate()
		// + ",'yyyy-MM-dd hh24:mi:ss')"

		ochannelDao.add(sql, param, true);
		return "新增成功!";
	}

	public String modify(Ochannel ochannel) {
		String sql = "update O_CHANNEL set " + "USERNAME=?,PWD=?,"
				+ "DSNAME=?,DSTYPE=?," + "DSSHORTCODE=?,DSCODE=?,"
				+ "AUTHBRAND=?,PRODUCTCATEGORY=?," + "BAIL=?,DBUSINESSUSER=?,"
				+ "DBUSINESSMODEL=?,MODIFYNAME=?,MODIFYDATE=?"
				+ "  where KEYID=?";//
		Object param[] = { ochannel.getUsername(), ochannel.getPwd(),
				ochannel.getDsname(), ochannel.getDstype(),
				ochannel.getDsshortcode(), ochannel.getDscode(),
				ochannel.getAuthbrand(), ochannel.getProductcategory(),
				ochannel.getBail(), ochannel.getDbusinessuser(),
				ochannel.getDbusinessmodel(), ochannel.getModifyname(),
				ochannel.getModifydate(), ochannel.getKeyid() };// ,
		ochannelDao.modify(sql, param, true);
		return "修改成功!";
	}

	public String isEnable(String keyId, String enableed) {
		String sql = "update O_CHANNEL set ISENABLE=?  where KEYID=?";
		Object param[] = { enableed, keyId };
		ochannelDao.isEnable(sql, param, true);
		if (StringUtils.isNotBlank(enableed) && enableed.equals("0")) {
			return "启用成功!";
		} else {
			return "禁用成功!";
		}
	}

	public String bathBrand(String keyIds, String bandCodes) {
		String[] keyArry = keyIds.split(",");

		StringBuffer sql = new StringBuffer();
		sql.append("update O_CHANNEL set AUTHBRAND=? where KEYID in");
		sql.append("(");
		for (int key = 0; key < keyArry.length; key++) {
			if (key == 0) {
				sql.append("'" + keyArry[key] + "'");
			} else {
				sql.append(",'" + keyArry[key] + "'");
			}
		}
		sql.append(")");
		Object param[] = { bandCodes };
		ochannelDao.modify(sql.toString(), param, true);
		return "授权成功";
	}

	public String delete(String keyId) {
		String sql = "delete from O_CHANNEL where KEYID=?";
		Object param[] = { keyId };
		ochannelDao.delete(sql, param, true);
		return "删除成功";
	}

	public Ochannel loadOchanel(String keyid) {
		String sql = "select * from O_CHANNEL where  KEYID=?";

		Ochannel ochannel = ochannelDao.find(sql, keyid, true);

		return ochannel;
	}

	public List<OchannelUtil> findQuery(OchannelVo qvo) {

		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("select * from  O_CHANNEL where  1=1");
		if (StringUtils.isNotBlank(qvo.getDsName())) {
			stringBuffer.append(" and DSNAME=" + qvo.getDsName());
		}

		if (StringUtils.isNotBlank(qvo.getAuthBrand())) {
			stringBuffer.append(" and AUTHBRAND=" + qvo.getAuthBrand());
		}

		if (StringUtils.isNotBlank(qvo.getDsType())) {
			stringBuffer.append(" and DSTYPE=" + qvo.getDsType());
		}

		if (StringUtils.isNotBlank(qvo.getDbusinessUser())) {
			stringBuffer.append(" and DBUSINESSUSER=" + qvo.getDbusinessUser());
		}

		if (StringUtils.isNotBlank(qvo.getDbusinessModel())) {
			stringBuffer.append(" and DBUSINESSMODEL="
					+ qvo.getDbusinessModel());
		}
		if (StringUtils.isNotBlank(qvo.getProductCategory())) {
			stringBuffer.append(" and PRODUCTCATEGORY="
					+ qvo.getProductCategory());
		}

		List<Ochannel> ochannelLs = ochannelDao.findQuery(
				stringBuffer.toString(), true);
		List<OchannelUtil> utils = new ArrayList<OchannelUtil>();
		for (int arry = 0; ochannelLs != null && ochannelLs.size() > 0
				&& arry < ochannelLs.size(); arry++) {
			Ochannel ochannel = ochannelLs.get(arry);
			OchannelUtil ochannelUtil = new OchannelUtil();

			ochannelUtil.setKeyid(ochannel.getKeyid());
			ochannelUtil.setUsername(ochannel.getUsername());
			ochannelUtil.setDsname(ochannel.getDsname());
			ochannelUtil.setAuthbrand(ochannel.getAuthbrand());
			ochannelUtil.setProductcategory(ochannel.getProductcategory());
			ochannelUtil.setBail(ochannel.getBail());
			ochannelUtil.setDbusinessuser(ochannel.getDbusinessuser());
			ochannelUtil.setCreatedate(ochannel.getStrsCreatedate());
			ochannelUtil.setIsEnable(ochannel.getIsenable());
			utils.add(ochannelUtil);
		}
		return utils;
	}
}
